package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.levelService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/product")
//@CrossOrigin
public class levelController {

    @Autowired
    public levelService levelService;

    //一级查询
    @RequestMapping("/getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> BaseCategory1List = levelService.getCategory1();
        return Result.ok(BaseCategory1List);
    }

    //二级查询
    @RequestMapping("/getCategory2/{category1id}")
    public Result getCategory2(@PathVariable Long category1id ){
        List<BaseCategory2> baseCategory2List = levelService.getCategory2(category1id);
        return Result.ok(baseCategory2List);
    }

    //三级查询
    @RequestMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){
        List<BaseCategory3> baseCategory3List = levelService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }

    //根据分类id获取平台属性
    @RequestMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable Long category1Id,
                               @PathVariable Long category2Id,
                               @PathVariable Long category3Id)
        {
        List<BaseAttrInfo> BaseAttrInfoList = levelService.attrInfoList(
                category1Id,category2Id,category3Id );
        return Result.ok(BaseAttrInfoList);
        }

     //添加平台属性   restFul 风格：  查询GET 修改PUT 添加POST 删除 DELETE
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        levelService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    //根据平台属性ID获取平台属性
    @GetMapping("/getAttrValueList/{attrId}")
    public Result<List<BaseAttrValue>> getAttrValueList(@PathVariable Long attrId){
        BaseAttrInfo baseAttrInfo = levelService.getAttrInfo(attrId);
        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(baseAttrValueList);
    }

    //获取品牌分页列表
    @GetMapping("//baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,
                                @PathVariable Integer limit){
        IPage iPage = levelService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }

    //品牌添加
    @PostMapping("/baseTrademark/save")
    public Result baseTrademark(@RequestBody BaseTrademark baseTrademark){
        levelService.baseTrademarksave(baseTrademark);
        return Result.ok();
    }
//     查询SPU的分页结果集
    @GetMapping("/{page}/{limit}")
    public Result spupage(@PathVariable Integer page ,
                          @PathVariable Integer limit,
                          Long category3Id){
        IPage iPage=levelService.spuPage(page,limit,category3Id);
        return Result.ok(iPage);
    }

    //添加spu 品牌下拉框
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = levelService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //添加SPU 之 销售属性下拉框
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = levelService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //SPU添加
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        levelService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //根据SpuID查询所有此商品的图片  bigint
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId) {
        List<SpuImage> spuImageList = levelService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //根据spuId获取销售属性
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = levelService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //保存SKU
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        levelService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //获取sku分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result skuPage(@PathVariable Integer page,
                       @PathVariable Integer limit){
        IPage iPage = levelService.skuPage(page,limit);
        return Result.ok(iPage);
    }

    //上架
    @PutMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        levelService.onSale(skuId);
        return Result.ok();
    }

    //下架
    @PutMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        levelService.cancelSale(skuId);
        return Result.ok();
    }
}
